<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="下单日期" style="width: 350px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="生产单编号" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="车间" prop="workshopName">
            <el-select
              style="width: 100%"
              v-model="queryParams.workshopName"
              placeholder="请选择"
            >
              <el-option
                v-for="item in ems_workshop"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              />
            </el-select>
          </el-form-item> -->
        <el-form-item label="产品名称" prop="shipmentType">
          <el-input
            v-model.trim="queryParams.shipmentType"
            style="width: 180px"
            placeholder="请输入产品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="出版编码" prop="pubNo">
          <el-input
            v-model.trim="queryParams.pubNo"
            style="width: 180px"
            placeholder="请输入出版编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="制版状态" prop="zbDate">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.zbDate"
            placeholder="请选择"
          >
            <el-option value="未制版" label="未制版"></el-option>
            <el-option value="已制版" label="已制版"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="印刷状态" prop="ysDate">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.ysDate"
            placeholder="请选择"
          >
            <el-option value="已排产" label="已排产"></el-option>
            <el-option value="印刷中" label="印刷中"></el-option>
            <el-option value="已完成" label="已完成"></el-option>
            <el-option value="--" label="--"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="折页状态" prop="zyDate">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.zyDate"
            placeholder="请选择"
          >
            <el-option value="已排产" label="已排产"></el-option>
            <el-option value="折页中" label="折页中"></el-option>
            <el-option value="已完成" label="已完成"></el-option>
            <el-option value="--" label="--"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装订状态" prop="zdDate">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.zdDate"
            placeholder="请选择"
          >
            <el-option value="已排产" label="已排产"></el-option>
            <el-option value="装订中" label="装订中"></el-option>
            <el-option value="已完成" label="已完成"></el-option>
            <el-option value="--" label="--"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货状态" prop="fhDate">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.fhDate"
            placeholder="请选择"
          >
            <el-option value="未发货" label="未发货"></el-option>
            <el-option value="已发货" label="已发货"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-select
            v-model="queryParams.workshopName"
            clearable
            style="width: 180px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in workshopOptions"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="版材尺寸" prop="sheetSize">
          <el-select
            filterable
            v-model="queryParams.sheetSize"
            clearable
            style="width: 180px"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ems_machine_info_size"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
              <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col> -->
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        height="65vh"
        :data="dataList"
        v-if="dataList"
        @expand-change="handleHeaderClick"
        :span-method="spanMethod"
        :summary-method="getSummaries"
        show-summary
      >
        <!-- <el-table-column label="序号" width="60" type="index" align="center" /> -->
        <el-table-column
          label="状态"
          min-width="90"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="数据状态"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          prop="isSuspend"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <el-tag
              :type="row.isSuspend == 1 ? 'error' : 'success'"
              effect="dark"
              round
            >
              {{ row.isSuspend == 1 ? "暂停" : "正常" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="100"
          label="生产单编号"
          prop="produceNo"
        >
          <template v-slot="{ row }">
            <el-button @click="handlePrint(row)" type="text">
              {{ row.produceNo }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          label="出版编码"
          prop="pubNo"
        >
        </el-table-column>
        <el-table-column
          min-width="200"
          align="center"
          label="产品名称"
          prop="shipmentType"
        />
        <el-table-column align="center" label="车间" prop="workshopName" />
        <el-table-column
          align="center"
          label="下单日期"
          min-width="70"
          prop="submitDate"
        >
          <template v-slot="{ row }">
            {{ row.submitDate && row.submitDate.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="70"
          label="交货日期"
          prop="deliverDate"
        >
          <template v-slot="{ row }">
            {{ row.deliverDate && row.deliverDate.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="品种" prop="breedType" />
        <el-table-column align="center" label="组成" prop="composition" />
        <el-table-column
          align="center"
          label="印张"
          prop="sheet"
          min-width="60"
        />
        <el-table-column
          align="center"
          label="分印张"
          prop="productSheet"
          min-width="60"
        />

        <!-- <el-table-column
                    align="center"
                    label="版材尺寸"
                    prop="sheetSize"
                  >
                    <template #default="scope">
                      <dict-tag
                        :options="ems_machine_info_size"
                        :value="scope.row.sheetSize"
                      />
                    </template> 
                  </el-table-column>-->

        <el-table-column
          align="center"
          label="订单数量"
          prop="orderCount"
          min-width="85"
        />
        <el-table-column
          align="center"
          label="用纸令数"
          prop="usePaperNum"
          min-width="85"
        />
        <!-- <el-table-column
                    align="center"
                    label="装订方式"
                    prop="bindingMode"
                  >
                    <template #default="scope">
                      <dict-tag
                        :options="ems_order_binding_mode"
                        :value="scope.row.bindingMode"
                      />
                    </template>
                  </el-table-column> -->
        <el-table-column align="center" label="制版状态" prop="zbDate">
          <template #default="scope">
            <span style="color: blue" v-if="scope.row.zbDate == '已完成'">
              {{ scope.row.zbDate }}</span
            >
            <span style="color: red" v-else-if="scope.row.zbDate == '--'">
              {{ scope.row.zbDate }}</span
            >
            <span v-else>
              {{ scope.row.zbDate }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="印刷状态" prop="ysDate">
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.ysDate == '已完成' || scope.row.ysDate == '印刷中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '1')"
                text
              >
                {{ scope.row.ysDate }}
              </el-button>
            </span>
            <span style="color: red" v-else-if="scope.row.ysDate == '--'">
              {{ scope.row.ysDate }}
            </span>
            <span v-else>
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '2')"
                text
              >
                {{ scope.row.ysDate }}
              </el-button>
            </span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="折页状态" prop="zyDate">
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.zyDate == '已完成' || scope.row.zyDate == '折页中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '2')"
                text
              >
                {{ scope.row.zyDate }}
              </el-button>
            </span>
            <span v-else-if="scope.row.zyDate == '已排产'">
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '4')"
                text
              >
                {{ scope.row.zyDate }}
              </el-button>
            </span>
            <span style="color: red" v-else>
              {{ scope.row.zyDate }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="装订状态" prop="zdDate">
          <template #default="scope">
            <span
              style="color: blue"
              v-if="
                scope.row.zdDate == '已完成' || scope.row.zdDate == '装订中'
              "
            >
              <el-button
                type="primary"
                @click="handleView(scope.row, '5')"
                text
              >
                {{ scope.row.zdDate }}
              </el-button>
            </span>
            <span v-else-if="scope.row.zdDate == '已排产'">
              <el-button
                type="primary"
                @click="handleViewLoading(scope.row, '3')"
                text
              >
                {{ scope.row.zdDate }}
              </el-button>
            </span>
            <span style="color: red" v-else> {{ scope.row.zdDate }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="发货数量" prop="fhDate">
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewCount(scope.row, '3')"
              text
            >
              {{ scope.row.fhDate }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="未入库用纸令数"
          prop="notInPaperNum"
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <printView
        :visible="printOpen"
        :printList="printList"
        @cancel="
          () => {
            printOpen = false;
            printList = [];
          }
        "
      ></printView>
    </div>
  </div>
</template>
  
  <script setup name="TextOrderProgress">
import { getMachineTask } from "@/api/production/cover.js";
import { getNWScheduledetail } from "@/api/production/orderProgress.js";
import { getMesWorkShop } from "@/api/basicInfo/workshop.js";
import printView from "@/components/PrintView";
const printOpen = ref(false);
const printList = ref([]);
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
const notInPaperNumSum = ref(0);
const fhDateNumSum = ref(0);
const usePaperNumSum = ref(0);
const workshopOptions = ref([]);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `新/旧版`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版编码`, visible: true },
  { key: 4, label: `生产单编码`, visible: true },
  { key: 5, label: `出版编码`, visible: true },
  { key: 6, label: `书刊编码`, visible: true },
  { key: 7, label: `书刊名称`, visible: true },
  { key: 8, label: `印刷序列号`, visible: true },
  { key: 9, label: `品种`, visible: true },
  { key: 10, label: `组成`, visible: true },
  { key: 11, label: `印张`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `印色`, visible: true },
  { key: 14, label: `版材数量`, visible: true },
  { key: 15, label: `制版数量`, visible: true },
  { key: 16, label: `版材尺寸`, visible: true },
  { key: 17, label: `制版人`, visible: true },
  { key: 18, label: `烤版人`, visible: true },
  { key: 19, label: `完成日期`, visible: true },
  { key: 20, label: `取版人`, visible: true },
  { key: 21, label: `取版日期`, visible: true },
  { key: 22, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});
const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  ems_workshop,
  ems_machine_info_size,
  ems_order_binding_mode,
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
} = proxy.useDict(
  "ems_workshop",
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "ems_machine_info_size",
  "ems_order_binding_mode"
);

const getList = () => {
  loading.value = true;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  getNWScheduledetail(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        usePaperNumSum.value = res.rows[0].usePaperNumTotal;
        notInPaperNumSum.value = res.rows[0].notInPaperNumTotal;
        fhDateNumSum.value = res.rows[0].fhDateNum;
      }

      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
  const fields = [
    "status",
    "isSuspend",
    "pubNo",
    "produceNo",
    "shipmentType",
    "workshopName",
    "submitDate",
    "deliverDate",
    "sheet",
    "orderCount",
    "fhDate",
    "usePaperNum",
    "notInPaperNum",
  ];
  const currentProduceNo = row.produceNo;
  const cellValue = row[column.property];
  if (cellValue && fields.includes(column.property)) {
    const prevRow = dataList.value[rowIndex - 1]; //上一行数据
    let nextRow = dataList.value[rowIndex + 1]; //下一行数据
    if (
      prevRow &&
      prevRow[column.property] === cellValue &&
      prevRow.produceNo === currentProduceNo
    ) {
      return { rowspan: 0, colspan: 1 };
    } else {
      let countRowspan = 1;
      while (
        nextRow &&
        nextRow[column.property] === cellValue &&
        nextRow.produceNo === currentProduceNo
      ) {
        nextRow = dataList.value[++countRowspan + rowIndex];
      }
      if (countRowspan > 1) {
        return { rowspan: countRowspan, colspan: 1 };
      }
    }
  }
};

// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }

    switch (column.property) {
      case "notInPaperNum":
        sums[index] = notInPaperNumSum.value;
        break;
      case "usePaperNum":
        sums[index] = usePaperNumSum.value;
        break;
      case "fhDate":
        sums[index] = fhDateNumSum.value;
        break;

      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const getWorkShopList = async () => {
  const res = await getMesWorkShop({ ...queryParams.value });
  workshopOptions.value = [...res.rows];
};
getWorkShopList();

const handleViewCode = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.platemakingId,
      info: true,
    },
  });
};

const handleView = (row, type) => {
  router.push({
    path: "/Productionmanagementdistribution/production/searchReportDetail",
    query: {
      breed: row.breedType,
      type: type,
      produceNo: row.produceNo,
      status: -2,
      info: true,
    },
  });
};

// 折页中 印刷中 覆膜中 裁切中
const handleViewLoading = (row, type) => {
  router.push({
    path: "/production/orderProgress/processPenetration",
    query: {
      breedType: row.breedType,
      machineType: type,
      produceNo: row.produceNo,
      info: true,
    },
  });
};

//发货数量
const handleViewCount = (row, type) => {
  router.push({
    path: "/production/orderProgress/quantityPenetration",
    query: {
      produceNo: row.produceNo,
      info: true,
    },
  });
};
const handlePrint = (row) => {
  printList.value = [row];
  printOpen.value = true;
};

getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>
  
  <style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
  